package com.cn.servlet.register;



import com.cn.Dao.DepDao;
import com.cn.Dao.UserDao;
import com.cn.entity.DepInfo;
import com.cn.entity.UserInfo;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String message = "";
        request.setCharacterEncoding("GBK");
        response.setCharacterEncoding("GBK");
        try {
            UserDao dao=new UserDao();
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String name=request.getParameter("name");
            String phone=request.getParameter("phone");
            String did=request.getParameter("departmentid");
            UserInfo userInfo=new UserInfo();
            userInfo.setUsername(username);
            userInfo.setPassword(password);
            userInfo.setName(name);
            userInfo.setPhone(phone);
            userInfo.setDepartmentid(Integer.valueOf(did));
            dao.Insert(userInfo);
            message="注册成功";
            //注册仅能作为普通用户登录，不能注册管理员
            DepDao depDao=new DepDao();
            List<DepInfo> depInfos=depDao.findAll();
            userInfo=dao.findByName(userInfo);
            request.getSession().setAttribute("userInfo",userInfo);
            request.getSession().setAttribute("depInfos",depInfos);
            request.getRequestDispatcher("user.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            message="注册失败";
        }finally {
            request.setAttribute("message", message);
        }
    }
}
